package eco.timer;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.PendingIntent;
import android.app.Service;
import android.app.PendingIntent.CanceledException;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class EcoTimerWidget extends AppWidgetProvider {
	// our actions for our buttons
	public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";

	static int mCurrentTipID = -1;
	public static final String TIP_ID = "TIP_ID";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// To prevent any ANR timeouts, we perform the update in a service
		// context.startService(new Intent(context, UpdateService.class));
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		for (int appWidgetId : appWidgetIds) {

			PendingIntent updatepending = EcoTimerWidget
					.makeControlPendingIntent(context, UpdateService.UPDATE,
							appWidgetId);
			try {
				updatepending.send();
			} catch (CanceledException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onDisabled(Context context) {
		context.stopService(new Intent(context, UpdateService.class));
		super.onDisabled(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// v1.5 fix that doesn't call onDelete Action
		final String action = intent.getAction();
		Log.w("onReceive() action", action);
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else {
			try {
				super.onReceive(context, intent);
			} catch (IndexOutOfBoundsException ex) {
				Log.e("onReceive", ex.getMessage());
				ex.printStackTrace();
			}
		}
	}

	public static class UpdateService extends Service {

		public static final String UPDATE = "update";
		public static final String OPEN_TIP = "open_tip";

		@Override
		public void onStart(Intent intent, int startId) {
			int id = intent.getIntExtra(EcoTimerWidget.TIP_ID, -1);
			mCurrentTipID = (id == -1) ? mCurrentTipID : id - 1;
			int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID);

			ComponentName thisWidget = new ComponentName(this,
					EcoTimerWidget.class);
			AppWidgetManager manager = AppWidgetManager.getInstance(this);

			RemoteViews updateViews = buildUpdate(this);

			/*
			updateViews.setOnClickPendingIntent(R.id.btnRefresh, EcoTimerWidget
					.makeControlPendingIntent(getApplicationContext(), UPDATE,
							appWidgetId));
*/
			
			// Create an Intent to launch ExampleActivity
			Intent openAppIntent = new Intent(this, EcoTimerActivity.class);
			openAppIntent.putExtra(TIP_ID, mCurrentTipID);
			openAppIntent.setAction(OPEN_TIP);
			PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
					openAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			
			/*
			updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
			manager.updateAppWidget(thisWidget, updateViews);
		*/
		}

		public RemoteViews buildUpdate(Context context) {

			RemoteViews updateViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_ecotips);


			/*s
			updateViews.setTextViewText(R.id.word_title, tip.mTitle);
			updateViews.setTextViewText(R.id.definition, tip.mDescription);
			*/
			return updateViews;
		}

		@Override
		public IBinder onBind(Intent intent) {
			// We don't need to bind to this service
			return null;
		}
	}

	public static PendingIntent makeControlPendingIntent(Context context,
			String command, int appWidgetId) {
		Intent active = new Intent(context, UpdateService.class);
		active.setAction(command);
		active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		// this Uri data is to make the PendingIntent unique, so it wont be
		// updated by FLAG_UPDATE_CURRENT
		// so if there are multiple widget instances they wont override each
		// other
		Uri data = Uri.withAppendedPath(Uri.parse("ecotipswidget://widget/id/#"
				+ command + appWidgetId), String.valueOf(appWidgetId));
		active.setData(data);
		return (PendingIntent.getService(context, 0, active,
				PendingIntent.FLAG_UPDATE_CURRENT));
	}
}
